package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerType;
import com.prs.crm.service.crm.CustomerTypeService;
import com.prs.crm.service.sys.LogService;

public class CustomerTypeAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private CustomerType customerType;
	private CustomerTypeService customerTypeService;
	private PaginationSupport pagination;
	private Integer[] customerTypeIds;
	private LogService logService;

	
	@Action("listCustomerType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(CustomerType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("selectCustomerType")
	public String selectCustomerType() {
		return this.execute();
	}
	
	@Action("editCustomerType")
	public String edit() {
		return SUCCESS;
	}
	
	@Action(value="saveCustomerType",results={@Result(name="success",type="redirect",location="listCustomerType")
			,@Result(name="error",type="chain",location="editCustomerType")})
	public String save() {
		if(customerType.getName()==null||"".equals(customerType.getName().trim())){
			this.addActionError("类型名称不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(CustomerType.class, customerType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getCustomerTypeService().save(customerType);
		return SUCCESS;
	}
	
	@Action(value="removeCustomerType",results={@Result(name="success",type="redirect",location="listCustomerType"),
			@Result(name="error",type="chain",location="listCustomerType")})
	public String remove() {
		if(customerTypeIds == null) {
			this.addActionError("请选择要删除的客户类型");
			return ERROR;
		}
		for(Integer id : customerTypeIds) {
			CustomerType type = this.getCustomerTypeService().get(id);
			if(type.getCustomers().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getCustomerTypeService().batchRemove(customerTypeIds);
		return SUCCESS;
	}
	
	public CustomerType getCustomerType() {
		return customerType;
	}
	public void setCustomerType(CustomerType customerType) {
		this.customerType = customerType;
	}
	public CustomerTypeService getCustomerTypeService() {
		return customerTypeService;
	}
	public void setCustomerTypeService(CustomerTypeService customerTypeService) {
		this.customerTypeService = customerTypeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getCustomerTypeIds() {
		return customerTypeIds;
	}
	public void setCustomerTypeIds(Integer[] customerTypeIds) {
		this.customerTypeIds = customerTypeIds;
	}
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
